package cn.jiangxiong.pethome.system.dept.controller;

import cn.jiangxiong.pethome.system.dept.domain.Department;
import cn.jiangxiong.pethome.system.dept.domain.DepartmentRo;
import cn.jiangxiong.pethome.system.dept.query.DepartmentQuery;
import cn.jiangxiong.pethome.system.dept.service.IDepartmentService;
import cn.jiangxiong.pethome.utils.AjaxResult;
import cn.jiangxiong.pethome.utils.BusinessException;
import cn.jiangxiong.pethome.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @PostMapping("/list")
    @ApiOperation(value = "获取所有不们信息")
    public List<Department> findAll(){
        return departmentService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门信息")
    public AjaxResult findOne(@PathVariable Long id){
        try {
            Department one = departmentService.findOne(id);
            return AjaxResult.success().setData(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    @PostMapping("/pagelist")
    @ApiOperation(value = "分页查询")
    public AjaxResult findByQuery(@RequestBody DepartmentQuery query){
        try {
            List<Department> byQuery = departmentService.findByQuery(query);
            Long count = departmentService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除部门")
    public AjaxResult delete(@PathVariable Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }
    @PutMapping
    @ApiOperation(value = "保存部门信息【有id则为修改】")
    public AjaxResult save(@RequestBody Department department) {
        try {
            if (department.getId() != null) {
                departmentService.update(department);

            } else {
                departmentService.add(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }
    //批量删除
    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除部门")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }

    @GetMapping("/tree")
    @ApiOperation(value = "返回部门树")
    public List<Department> getDeptTree(){
        return departmentService.deptTree();
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Department> all = departmentService.findAll();
        ExcelUtil.exportExcel(all,"部门数据表","部门数据",Department.class,"导出的部门数据.xls",response);
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Department> departments = ExcelUtil.importExcel(file, 1, 1, Department.class);
        departments.forEach(System.out::println);
    }

    @GetMapping("/countByDate")
    public AjaxResult countByDate(){
        try {
            List<DepartmentRo> departmentRos = departmentService.countByDate();
            return AjaxResult.success().setData(departmentRos);
        }catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络繁忙，稍后再试！");
        }
    }
}
